package zjut.raymond.witdemo.util;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.util.Log;

import androidx.annotation.RequiresApi;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public abstract class WifiScanUtils {
    public static final String TAG = "WifiScanUtils";
    private WifiManager wifiManager;
    public ScheduledExecutorService executorService;
    private Context context;

    public WifiScanUtils(Context context) {
        this.context = context;
        wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        context.registerReceiver(wifiScanReceiver, intentFilter);
        executorService = Executors.newScheduledThreadPool(10);
        executorService.scheduleAtFixedRate(runnable, 0, 32, TimeUnit.SECONDS);
    }

    public Context getContext() {
        return context;
    }

    BroadcastReceiver wifiScanReceiver = new BroadcastReceiver() {
        @RequiresApi(api = Build.VERSION_CODES.M)
        @Override
        public void onReceive(Context context, Intent intent) {
            boolean success = intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false);
            if (success) {
                scanSuccess();
            } else {
                scanFailure();
            }
        }
    };

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            //开始扫描
            Log.d(TAG, "run: 开始扫描");
            if (!wifiManager.isWifiEnabled()) {
                wifiManager.setWifiEnabled(true);
            }
            wifiManager.startScan();
        }
    };

    private void scanSuccess() {
        List<ScanResult> results = wifiManager.getScanResults();
        Collections.sort(results, (o1, o2) -> {
            //  return 1 if rhs should be before lhs
            //  return -1 if lhs should be before rhs
            //  return 0 otherwise
            if (o1.level > o2.level) {
                return -1;
            } else if (o1.level < o2.level) {
                return 1;
            }
            return 0;
        });
        processScanResult(results);
    }

    private void scanFailure() {
        List<ScanResult> results = wifiManager.getScanResults();
        Collections.sort(results, new Comparator<ScanResult>() {
            @Override
            public int compare(ScanResult o1, ScanResult o2) {
                //  return 1 if rhs should be before lhs
                //  return -1 if lhs should be before rhs
                //  return 0 otherwise
                if (o1.level > o2.level) {
                    return -1;
                } else if (o1.level < o2.level) {
                    return 1;
                }
                return 0;
            }
        });
        Log.d(TAG, "scanFailure: 扫描失败");
    }

    abstract public void processScanResult(List<ScanResult> results);


    public void destroyed() {
        Log.d(TAG, "destroyed: 已销毁");
        executorService.shutdown();
        getContext().unregisterReceiver(wifiScanReceiver);
    }
}
